package com.difsonapps.android.joinme;

import com.difsonapps.android.joinme.R;
import com.difsonapps.android.reimpl.CustomCursorAdapter;

import android.app.Fragment;
import android.app.ListActivity;
import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentResolver;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ProgressBar;


public class ContactsFragment extends ListFragment implements
		LoaderCallbacks<Cursor> {
	// This is the Adapter being used to display the list's data
	CustomCursorAdapter mAdapter;
	   private final String TAG = this.getClass().getSimpleName();
	// These are the Contacts rows that we will retrieve
	static final String[] PROJECTION = new String[] {
			ContactsContract.Data._ID, 
			ContactsContract.Data.PHOTO_THUMBNAIL_URI,
			ContactsContract.Data.DISPLAY_NAME};

	// This is the select criteria
	static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME+ " NOTNULL) AND (" + ContactsContract.Data.DISPLAY_NAME+ " != '' ) AND (" +ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER+ " > 0))";

	public ContactsFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_contacts, container,
				false);

		return rootView;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	    // Create a progress bar to display while the list loads
        ProgressBar progressBar = new ProgressBar(getActivity().getApplicationContext());
        progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.CENTER));
        progressBar.setIndeterminate(true);
        getListView().setEmptyView(progressBar);

        // Must add the progress bar to the root of the layout
       // ViewGroup root = (ViewGroup) getView().findViewById(android.R.id.content);
        //root.addView(progressBar);

        // For the cursor adapter, specify which columns go into which views
        String[] fromColumns = {ContactsContract.Data.PHOTO_THUMBNAIL_URI, ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
       // int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

        int[] toViews = { R.id.iv_photo,R.id.tv_name, R.id.tv_status};
        // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new CustomCursorAdapter(getActivity().getApplicationContext(),   R.layout.contact_list, null,fromColumns, toViews, 0);
        setListAdapter(mAdapter);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState); 
	}

	@Override
	public android.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
		// TODO Auto-generated method stub

		return new CursorLoader(getActivity().getApplicationContext(), ContactsContract.Data.CONTENT_URI,
				null, SELECTION, null, null);
	}

	@Override
	public void onLoadFinished(android.content.Loader<Cursor> loader,
			Cursor data) {
		// TODO Auto-generated method stub
		Log.i(TAG, "loader "+data.getCount());
		ContentResolver cr = getActivity().getContentResolver();
		 mAdapter.swapCursor(data,cr);
	}

	@Override
	public void onLoaderReset(android.content.Loader<Cursor> loader) {
		// TODO Auto-generated method stub
		
		   mAdapter.swapCursor(null);
	}


}
